package org.lzy.shop.adapter

import android.R.attr.data
import android.annotation.SuppressLint
import android.content.Context
import android.view.ViewGroup
import android.widget.CheckBox
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.response.OrderRefundReason

class OrderRefundReasonAdapter: BaseQuickAdapter<OrderRefundReason, QuickViewHolder>()  {

    private var selectedPosition = -1

    /** 设置选中位置，并清除之前选中  */
    fun setSelectedPosition(position: Int) {
        selectedPosition = position
    }


    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_order_refund, parent)
    }

    override fun onBindViewHolder(holder: QuickViewHolder, @SuppressLint("RecyclerView") position: Int, item: OrderRefundReason?) {
        item?.let {
            holder.setText(R.id.refund_name, item.name)
            val refundCbView = holder.getView<CheckBox>(R.id.refund_cb)
            refundCbView.setOnCheckedChangeListener(null)
            refundCbView.isChecked = position == selectedPosition
            refundCbView.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    // 若选中，记录位置并通知刷新，确保只有一个被选中
                    selectedPosition = position
                    notifyDataSetChanged()
                } else {
                    // 如果取消选中（手动取消），将 selectedPosition 重置为 -1
                    if (selectedPosition == position) {
                        selectedPosition = -1
                    }
                }
            }



        }



    }


}